import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.transfer模块已被废弃，替代模块为qturboframe.public_ui.h_ui.transfer", DeprecationWarning)

import re

from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.button_tag import ButtonTag
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.li_tag import LiTag
from qturboframe.lib.ranorex.web_element.span_tag import SpanTag
from qturboframe.lib.utils.check import CheckBasic
from ..ucf_base_element import BaseElement


class Transfer(BaseElement, CheckBasic):

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        self._element = DivTag(self._element.control)
        self.left_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-list\'][1]', timeout=self.timeout).control)
        self.right_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-list\'][-1]', timeout=self.timeout).control)
        self.operate_element = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-transfer-operation\']', timeout=self.timeout).control)

    def set_value_for_multiple(self, value=['all']):
        '''
        hui的穿梭框选择

        :Args:
         - value: 选择项的文本集合

        :Example:
         - 例: transfer.set_value_for_multiple(['苹果','葡萄'])
        '''
        re_checked = 'h-checkbox-checked'
        if len(value) == 1 and value[0] == 'all':
            all_right_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
                'xpath', './button[@class~\'h-btn-ghost\']', timeout=self.timeout)[1].control)
            all_right_btn.secure_click()
        else:
            for text in value:
                select_li = LiTag(self.left_element.find_element_by_attribute(
                    'xpath', './/ul[@class~\'h-transfer-list-content\']/'
                             'li[@class~\'h-transfer-list-content-item\' and @innertext=\'{}\']'.format(text),
                    timeout=self.timeout).control)
                span = SpanTag(select_li.find_element_by_attribute(
                    'xpath', './/span[@class~\'h-checkbox\']', timeout=self.timeout).control)
                if not re.search(re_checked, span.get_attribute('class')):
                    span.secure_click()
            right_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
                'xpath', './button[@class~\'h-btn-primary\']', timeout=self.timeout)[1].control)
            right_btn.secure_click()

    def is_default(self, value):
        '''
        hui的穿梭框默认值校验

        :Args:
         - value: 校验的默认值集合

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: transfer.is_default(['苹果','葡萄'])
        '''
        li_list = [LiTag(webelement.control) for webelement in self.right_element.find_elements_by_attribute(
            'xpath',
            './/ul[@class~\'h-transfer-list-content\'//li[@class~\'h-transfer-list-content-item\']',
            timeout=self.timeout)]
        if len(value) == len(li_list):
            for li in li_list:
                if li.text.strip() not in value:
                    return False
            else:
                return True
        else:
            return False

    def clear(self):
        '''
        清除已选项

        :Example:
         - 例: transfer.clear()
        '''
        all_left_btn = ButtonTag(self.operate_element.find_elements_by_attribute(
            'xpath', './button[@class~\'h-btn-ghost\']', timeout=self.timeout)[0].control)
        all_left_btn.secure_click()

    def check_default(self, value):
        '''
        hui的穿梭框默认值校验

        :Args:
         - value: 校验的默认值集合

        :Raises:
            SeleniumError: 穿梭框的默认值校验不通过

        :Example:
         - 例: transfer.check_default(['苹果','葡萄'])
        '''
        if not self.is_default(value):
            raise CsClientError('穿梭框的默认值校验不通过！')

